package pers.lc.testDataGenerator.generator;

import pers.lc.testDataGenerator.data.ColumnData;
import pers.lc.testDataGenerator.data.ColumnInfo;
import pers.lc.testDataGenerator.data.GeneratedData;
import pers.lc.testDataGenerator.exception.DataException;
import pers.lc.testDataGenerator.utils.CollectionUtils;
import pers.lc.testDataGenerator.utils.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 生成测试数据
 */
public class DataGenerator {

    public static List<ColumnInfo> generatorOne(Collection<ColumnData> columnDataList){
        List<ColumnInfo> result = new ArrayList<>();
        columnDataList.forEach(s->{
            if (StringUtils.isNotBlank(s.getColumnValue())){
                result.add(ColumnGenerator.generatorByValue(s));
            } else if (CollectionUtils.isNotEmpty(s.getColumnSet())&&StringUtils.isNotBlank(s.getColumnSetRule().getType())) {
                result.add(ColumnGenerator.generatorSingleBySet(s));
            } else if (StringUtils.isNotBlank(s.getColumnValueFrom())&&StringUtils.isNotBlank(s.getColumnValueTo())&&StringUtils.isNotBlank(s.getColumnRangeRule().getType())) {
                result.add(ColumnGenerator.generatorSingleByRange(s));
            }else {
                throw  new DataException("字段生成规则未找到："+s.getColumnName());
            }
        });
        return result;
    }

    public static List<GeneratedData> generatorMulti(Collection<ColumnData> columnDataList, Integer size){
        List<GeneratedData> result = new ArrayList<>();
        int count = 0;
        while(count++<size){
            int finalCount = count-1;
            List<ColumnInfo> columnInfoList = new ArrayList<>();
            columnDataList.forEach(s->{
                if (StringUtils.isNotBlank(s.getColumnValue())){
                    columnInfoList.add(ColumnGenerator.generatorByValue(s));
                } else if (CollectionUtils.isNotEmpty(s.getColumnSet())&&StringUtils.isNotBlank(s.getColumnSetRule().getType())) {
                    columnInfoList.add(ColumnGenerator.generatorMultiBySet(s, finalCount));
                } else if (StringUtils.isNotBlank(s.getColumnValueFrom())&&StringUtils.isNotBlank(s.getColumnValueTo())&&StringUtils.isNotBlank(s.getColumnRangeRule().getType())) {
                    columnInfoList.add(ColumnGenerator.generatorMultiByRange(s,finalCount));
                }else {
                    throw  new DataException("字段生成规则未找到："+s.getColumnName());
                }
            });
            result.add(new GeneratedData(columnInfoList));
        }
        return result;
    }
}
